part of 'index.dart';

class PhotoViewPage extends StatefulWidget {
  const PhotoViewPage({super.key, this.index = 0, required this.list});
  final int index;
  final List<String> list;

  @override
  State<PhotoViewPage> createState() => _PhotoViewPageState();
}

class _PhotoViewPageState extends State<PhotoViewPage> {
  int currentIndex = 0;

  @override
  void initState() {
    currentIndex = widget.index;
    Tools.unfocus();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: Colors.black,
      appBar: AppBar(
        leading: const BackButton(color: Colors.white),
        backgroundColor: Colors.black,
        title: Text(
          '${currentIndex + 1}/${widget.list.length}',
          style: const TextStyle(color: Colors.white, fontSize: 15),
        ),
      ),
      body: PhotoViewGallery.builder(
        scrollPhysics: const BouncingScrollPhysics(),
        itemCount: widget.list.length,
        pageController: PageController(initialPage: widget.index),
        onPageChanged: (int index) {
          setState(() => currentIndex = index);
        },
        builder: (BuildContext context, int index) {
          final url = widget.list[index];
          return PhotoViewGalleryPageOptions(
            imageProvider: url.startsWith('http')
                ? CachedNetworkImageProvider(url)
                : FileImage(File(url)),
            initialScale: PhotoViewComputedScale.contained,
            minScale: PhotoViewComputedScale.contained,
            maxScale: PhotoViewComputedScale.covered * 4,
            heroAttributes: PhotoViewHeroAttributes(tag: index),
            onTapUp: (context, details, controllerValue) {
              Navigator.pop(context);
            },
          );
        },
        loadingBuilder: (context, event) => Center(
          child: CircularProgressIndicator(
            backgroundColor: const Color(0xFFDCD4FF),
            value: event == null
                ? 0
                : event.cumulativeBytesLoaded / (event.expectedTotalBytes ?? 0),
          ),
        ),
      ),
    );
  }
}
